/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog.automatic;

import com.googlecode.jaden.common.other.JadenException;
import com.googlecode.jaden.common.other.Utils;
import com.googlecode.jaden.ui.Constants;
import com.googlecode.jaden.ui.UiUtils;
import com.googlecode.jaden.ui.dialog.BaseDialog;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.prefs.Preferences;

public class AutomaticColumnSelectorDialog extends BaseDialog {

    private final DefaultComboBoxModel<String> outputComboModel = new DefaultComboBoxModel<String>();
    private final List<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
    private final List<ColumnMetaData> columnMetaDataList;
    private final AtomicInteger outputColumnNumber;
    private final JComboBox<String> outputCombo;

    public AutomaticColumnSelectorDialog(AutomaticProjectCreateDialog dialog, Preferences preferences,
                                         List<ColumnMetaData> columnMetaDataList, AtomicInteger outputColumnNumber) {

        super(dialog, Constants.DIALOG_AUTOMATIC_COLUMNS_TITLE);

        this.columnMetaDataList = columnMetaDataList;
        this.outputColumnNumber = outputColumnNumber;

        JPanel gridPanel = new JPanel(new GridLayout(0, 1));
        for (ColumnMetaData data : columnMetaDataList) {
            String heading = data.getHeading();
            JCheckBox checkBox = new JCheckBox(heading);
            checkBoxes.add(checkBox);
            checkBox.setSelected(true);
            gridPanel.add(checkBox);
            outputComboModel.addElement(heading);
            outputComboModel.setSelectedItem(heading);
        }
        JScrollPane scrollPane = new JScrollPane(UiUtils.horizontalList(gridPanel));
        scrollPane.setPreferredSize(new Dimension(150, 200));

        outputCombo = new JComboBox<String>(outputComboModel);
        JPanel mainPanel = UiUtils.verticalList(
                UiUtils.horizontalList(scrollPane),
                UiUtils.horizontalList(new JLabel(Utils.translate(Constants.LABEL_OUTPUT_COLUMN)), outputCombo)
        );
        setMainPanel(mainPanel, preferences);
    }

    protected void doOk() throws JadenException {
        String item = (String) outputComboModel.getSelectedItem();
        boolean validSelection = false;
        int selectedColumnCount = 0;
        for (JCheckBox checkBox : checkBoxes) {
            if (checkBox.isSelected()) {
                selectedColumnCount++;
                if (item.equals(checkBox.getText())) {
                    validSelection = true;
                }
            }
        }

        if (!validSelection) {
            JOptionPane.showMessageDialog(this, Utils.translate(Constants.ERROR_BAD_OUTPUT_COLUMN_SELECTION, item));
            return;
        }

        if (selectedColumnCount < 2) {
            JOptionPane.showMessageDialog(this, Utils.translate(Constants.ERROR_TOO_FEW_COLUMN_SELECTIONS, item));
            return;
        }

        // Keep the column meta data for the selected columns.
        List<ColumnMetaData> columnMetaDataListCopy = new ArrayList<ColumnMetaData>();
        int outputColumnCounter = 0;
        for (JCheckBox checkBox : checkBoxes) {
            if (checkBox.isSelected()) {
                String checkBoxText = checkBox.getText();
                for (ColumnMetaData data : columnMetaDataList) {
                    if (checkBoxText.equals(data.getHeading())) {
                        columnMetaDataListCopy.add(data);
                    }
                }
                if (outputCombo.getSelectedItem().equals(checkBoxText)) {
                    outputColumnNumber.set(outputColumnCounter);
                }
                outputColumnCounter++;
            }
        }

        columnMetaDataList.clear();
        columnMetaDataList.addAll(columnMetaDataListCopy);

        dispose();
    }
}
